//
//  BasseViewController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/13.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "BasseViewController.h"

@interface BasseViewController ()
@property (nonatomic,strong) UIImageView* noDataView;

@end

@implementation BasseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =UIColorFromRGB(0xFFFFFF);
    //是否显示返回按钮
    self.isShowLiftBack = YES;
    self.StatusBarStyle = UIStatusBarStyleDefault;
    //    //把导航栏的穿透效果关闭
    //    self.navigationController.navigationBar.translucent = NO;
    if (CurrentSystemVersion < 11.0) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.navigationController.navigationBar.tintColor = UIColorFromRGB(0x273323);
    //    [navBar setTintColor:KWhiteColor];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage createImageWithColor:KWhiteColor] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName :UIColorFromRGB(0x273323), NSFontAttributeName : PingFangSC_Medium(18)}];
}
- (UIStatusBarStyle)preferredStatusBarStyle{
    return _StatusBarStyle;
}
//动态更新状态栏颜色
-(void)setStatusBarStyle:(UIStatusBarStyle)StatusBarStyle{
    _StatusBarStyle=StatusBarStyle;
    [self setNeedsStatusBarAppearanceUpdate];
}
-(void)changeTextAction{
    DLog(@"%s",__func__);
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}
/**设置导航栏背景透明*/
-(void)setNavBarBackgroundAndClearColor{
    [self setHideNavBarBackgroundColor:NO HideColor:KClearColor appearColor:KClearColor];
    [self.navigationController.navigationBar setShadowImage:[UIImage createImageWithColor:KClearColor]];
}
/**设置导航栏背景非透明*/
-(void)setNavBarBackgroundAndNotClearColor{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage createImageWithColor:UIColor.whiteColor] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage createImageWithColor:UIColorFromRGB(0xE9EEE7)]];

}
/**设置根视图为登录页*/
-(void)setRootContollerToLoginController{
    kRootViewController = nil;
    kRootViewController =[[BasseNavigationController alloc] initWithRootViewController:[LoginViewController new]];
    
}
-(void)showNoDataImage{
    kWeakSelf(weakSelf);
    self.noDataView=[[UIImageView alloc] init];
    [self.noDataView setImage:[UIImage imageNamed:@"generl_nodata"]];
    [self.view.subviews enumerateObjectsUsingBlock:^(UITableView* obj, NSUInteger idx, BOOL *stop) {
        if ([obj isKindOfClass:[UITableView class]]) {
            [weakSelf.noDataView setFrame:CGRectMake(0, 0,obj.frame.size.width, obj.frame.size.height)];
            [obj addSubview:weakSelf.noDataView];
        }
    }];
}
-(void)removeNoDataImage{
    if (_noDataView) {
        [_noDataView removeFromSuperview];
        _noDataView = nil;
    }
}
/**
 *  是否显示返回按钮
 */
- (void)setIsShowLiftBack:(BOOL)isShowLiftBack
{
    _isShowLiftBack = isShowLiftBack;
    NSInteger VCCount = self.navigationController.viewControllers.count;
    //下面判断的意义是 当VC所在的导航控制器中的VC个数大于1 或者 是present出来的VC时，才展示返回按钮，其他情况不展示
    if (isShowLiftBack && ( VCCount > 1 || self.navigationController.presentingViewController != nil)) {
        [self addNavigationItemWithImageNames:@[@"back"] isLeft:YES target:self action:@selector(backBtnClicked) tags:nil];
    } else {
        self.navigationItem.hidesBackButton = YES;
        UIBarButtonItem * NULLBar=[[UIBarButtonItem alloc]initWithCustomView:[UIView new]];
        self.navigationItem.leftBarButtonItem = NULLBar;
    }
}
- (void)backBtnClicked
{
    [PresentationObject sharedPresentationObject].subViewFrame = CGRectMake(0, KScreenHeight - 240, KScreenWidth, 240);
    HideHubView;
    if (self.presentingViewController) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}
/**返回到指定控制器*/
-(void)popViewController:(Class )toVClass{
    BOOL isAre = NO;
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:toVClass]) {
            isAre = YES;
            [self.navigationController popToViewController:controller animated:YES];
        }
    }
    if (!isAre) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

- (void)addNavigationItemWithImageNames:(NSArray *)imageNames isLeft:(BOOL)isLeft target:(id)target action:(SEL)action tags:(nullable NSArray *)tags
{
    NSMutableArray * items = [[NSMutableArray alloc] init];
    NSInteger i = 0;
    for (NSString * imageName in imageNames) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        btn.frame = CGRectMake(0, 0, 30, 30);
        if(isLeft){
            [btn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 32)];
//            [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 3, 0, 0)];
            [btn setTitle:@"" forState:UIControlStateNormal];
            btn.frame = CGRectMake(0, 0, 50, 30);
            btn.clickEdgeInsets = UIEdgeInsetsMake(150, 150, 150, 150);
        }else{
            [btn setImageEdgeInsets:UIEdgeInsetsMake(0, 10, 0, -10)];
        }
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        
        btn.tag = [tags[i++] integerValue];
        UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:btn];
        [items addObject:item];
    }
    if (isLeft) {
        self.navigationItem.leftBarButtonItems = items;
    } else {
        self.navigationItem.rightBarButtonItems = items;
    }
}

- (void)addNavigationItemWithTitles:(NSArray *)titles isLeft:(BOOL)isLeft target:(id)target action:(SEL)action tags:(NSArray *)tags
{
    NSMutableArray * items = [[NSMutableArray alloc] init];
    //调整按钮位置
    UIBarButtonItem* spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    //将宽度设为负值
    spaceItem.width= -5;
    [items addObject:spaceItem];
    
    NSInteger i = 0;
    for (NSString * title in titles){
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, 30, 20);
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitle:title forState:UIControlStateSelected];
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = SYSTEMFONT(14);
        [btn setTitleColor:RGB(56,56,58) forState:UIControlStateNormal];
        [btn setTitleColor:RGB(56,56,58) forState:UIControlStateSelected];
        //        [btn setBackgroundImage:[UIImage createImageWithColor:[UIColor clearColor]] forState:UIControlStateSelected];
        [btn setBackgroundImage:[UIImage createImageWithColor:[UIColor clearColor]] forState:UIControlStateNormal];
        
        btn.tag = [tags[i++] integerValue];
        [btn sizeToFit];
        UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:btn];
        [items addObject:item];
    }
    if (isLeft) {
        self.navigationItem.leftBarButtonItems = items;
    } else {
        self.navigationItem.rightBarButtonItems = items;
    }
}

/**
 设置导航栏背景颜色
 
 @param isHide yes 影藏  no 显示
 @param hidecolor 影藏的颜色
 @param appearcolor 显示的颜色
 */
-(void)setHideNavBarBackgroundColor:(BOOL)isHide HideColor:(UIColor *)hidecolor appearColor:(UIColor *)appearcolor{
    if (!isHide) {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage createImageWithColor:appearcolor ? appearcolor : RGBA(231, 214, 183, 1)] forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setShadowImage:[UIImage createImageWithColor:RGB(200, 186, 173)]];
    }else{
        [self.navigationController.navigationBar setBackgroundImage:[UIImage createImageWithColor:hidecolor ? hidecolor : RGBA(255, 255, 255, 1)] forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    }
}

/**
 获取验证码
 */
-(void)getDownAction:(UILabel *)label{
    
    self.downTimer =  [self startTime:60 Target:label waitBlock:^(NSInteger remainTime) {
        DLog(@"%ld",(long)remainTime);
        label.text = [NSString stringWithFormat:@"(%zds)",remainTime];
        //        weakSelf.isCountDown = YES;
    } finishBlock:^{
        label.text = @"获取验证码";
        //        weakSelf.isCountDown = NO;
    }];
}
- (void)dealloc{
    if (self.downTimer != nil) {
        dispatch_source_cancel(self.downTimer);
    }
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(UIImageView*)bg_ImageView{
    if(!_bg_ImageView){
        _bg_ImageView=[UIImageView new];
        _bg_ImageView.image = IMAGE_NAMED(@"login-bj");
        _bg_ImageView.userInteractionEnabled = YES;
        [_bg_ImageView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapBgImageAction)]];
    }
    return _bg_ImageView;
}
-(void)tapBgImageAction{
    [self.view endEditing:YES];
}
//默认为YES，关闭为NO
-(void)setIQKeyboardManagerEnable:(BOOL)isEnable{
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
     manager.enable = isEnable;
    manager.enableAutoToolbar = isEnable;
}

@end
